package com.qf.agro.utils;

import com.qf.agro.common.sys.SysUserHolder;
import com.qf.agro.pojo.device.SmartDevice;
import com.qf.agro.pojo.device.SmartDeviceLog;
import com.qf.agro.pojo.sys.SysUser;
import com.qf.agro.service.device.SmartDeviceLogService;
import com.qf.agro.service.device.SmartDeviceService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class SmartLogAspect {
    @Autowired
    private SmartDeviceLogService smartDeviceLogService;
    @Autowired
    private SmartDeviceService smartDeviceService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 环绕通知
     */
    @Around("execution(* com.qf.agro.web.controller.device.SmartDeviceController.updateStateById(..))")
    public Object logAroundAdvice(ProceedingJoinPoint pjp) {
        //1.定义返回值
        Object rtValue = null;
        try {
            SysUser user = SysUserHolder.get();
            if (user != null) {
                Object[] args = pjp.getArgs();
                Long deviceId = null;
                for (Object arg : args) {
                    if (arg instanceof Long) {
                        deviceId = (Long) arg;
                        break;
                    }
                }
                if (deviceId != null) {
                    SmartDevice service = smartDeviceService.findById(deviceId);
                    SmartDeviceLog log = new SmartDeviceLog();
                    log.setDeviceId(deviceId);
                    log.setFacilitiesId(service.getFacilitiesId());
                    log.setDeviceName(service.getDeviceName());
                    log.setFacilitiesName(service.getFacilitiesName());
                    log.setDeviceName(service.getDeviceName());
                    if (service.getState() == 1) {
                        log.setCode("关闭设备");
                    } else {
                        log.setCode("打开设备");
                    }
                    log.setSendTime(new Date());
                    smartDeviceLogService.save(log);
                }
            }
            //2.取出执行方法所需的参数
            Object[] args = pjp.getArgs();
            //3.执行方法
            rtValue = pjp.proceed(args);
        }catch(Throwable t){
            t.printStackTrace();
        }
        //4.返回
        return rtValue;
    }

    /**
     * 获取action的内容
     * @param method
     * @return
     */
    private String getAction(Method method){
        //1.判断方法上有没有我想要的注解：GetMapping,PostMapping,PutMapping,DeleteMapping RequestMapping
        if(method.isAnnotationPresent(GetMapping.class)){
            //2.取出方法上的注解
            GetMapping getMapping = method.getAnnotation(GetMapping.class);
            //3.获取name属性
            return  getMapping.name();
        }

        if(method.isAnnotationPresent(PostMapping.class)){
            //2.取出方法上的注解
            PostMapping getMapping = method.getAnnotation(PostMapping.class);
            //3.获取name属性
            return  getMapping.name();
        }

        if(method.isAnnotationPresent(PutMapping.class)){
            //2.取出方法上的注解
            PutMapping getMapping = method.getAnnotation(PutMapping.class);
            //3.获取name属性
            return  getMapping.name();
        }

        if(method.isAnnotationPresent(DeleteMapping.class)){
            //2.取出方法上的注解
            DeleteMapping getMapping = method.getAnnotation(DeleteMapping.class);
            //3.获取name属性
            return  getMapping.name();
        }
        if(method.isAnnotationPresent(RequestMapping.class)){
            //2.取出方法上的注解
            RequestMapping getMapping = method.getAnnotation(RequestMapping.class);
            //3.获取name属性
            return  getMapping.name();
        }
        return null;
    }
}